/**
 * 
 */
package com.tw.trips.domain;

import java.util.HashSet;
import java.util.Set;

import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;

/**
 * @author m1005264
 * 
 */
@NodeEntity
public class Traveller {

	@GraphId
	Long nodeiD;

	@Indexed
	String emailID;

	private boolean isRegistered;

	private String name;

	private String password;

	@RelatedTo(type = "CREATED_BY", elementClass = Trip.class)
	@Fetch
	private Set<Trip> trips;

	@RelatedTo(elementClass = Update.class, type = "UPDATED", direction = Direction.INCOMING)
	@Fetch
	Set<Update> updates;

	@RelatedTo
	Set<Traveller> friends;

	@RelatedTo
	Set<Location> favLocations;

	@RelatedTo(type = "ALERT", elementClass = Alert.class, direction = Direction.INCOMING)
	@Fetch
	private Set<Alert> alerts;

	public Traveller(String email) {
		this.emailID = email;
	}

	public Traveller() {
		super();
	}

	public void addUpdate(Update update) {
		update.setUpdatedBy(this);
	}

	public Traveller(boolean isRegistered, String name, String password,
			String email) {
		this.isRegistered = isRegistered;
		this.name = name;
		this.password = password;
		this.emailID = email;
	}

	public Long getNodeiD() {
		return nodeiD;
	}

	public void setNodeiD(Long nodeiD) {
		this.nodeiD = nodeiD;
	}

	public String getEmailID() {
		return emailID;
	}

	public void setEmailID(String emailID) {
		this.emailID = emailID;
	}

	public boolean isRegistered() {
		return isRegistered;
	}

	public void setRegistered(boolean isRegistered) {
		this.isRegistered = isRegistered;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<Trip> getTrips() {
		return trips;
	}

	public Set<Update> getUpdates() {
		return updates;
	}

	public Set<Traveller> getFriends() {
		if (friends == null) {
			friends = new HashSet<Traveller>();
		}
		return friends;
	}

	public Set<Location> getFavLocations() {
		if (favLocations == null) {
			favLocations = new HashSet<Location>();
		}
		return favLocations;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((emailID == null) ? 0 : emailID.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Traveller other = (Traveller) obj;
		if (emailID == null) {
			if (other.emailID != null)
				return false;
		} else if (!emailID.equals(other.emailID))
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Traveller [nodeiD=" + nodeiD + ", emailID=" + emailID
				+ ", isRegistered=" + isRegistered + ", name=" + name
				+ ", password=" + password + ", trips size = "
				+ (trips != null ? trips.size() : 0) + ", updates size = "
				+ (updates != null ? updates.size() : 0) + ", friends="
				+ friends + ", favLocations=" + favLocations + " Alerts = "+alerts+"]";
	}

	public Set<Alert> getAlerts() {
		return alerts;
	}

	public void setAlerts(Set<Alert> alerts) {
		this.alerts = alerts;
	}

}
